import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/controller/reports.dart';
import 'package:ichinesereader/models/assessment/assessmentBookType.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/grids/testRecordGrid.dart';
import '../../themes/appTheme.dart';

class TeacherReportsQuizBook extends StatefulWidget {
  TeacherReportsQuizBook({Key? key}) : super(key: key);
  static const String routeName = '/teacher/reports/quizBook';

  @override
  _TeacherReportsQuizBook createState() => _TeacherReportsQuizBook();
}

class _TeacherReportsQuizBook extends State<TeacherReportsQuizBook> {
  final ReportsController _reportsController = Get.find();
  var box = Hive.box('appconfig');
  String classId = '';

  @override
  void initState() {
    classId = box.get(CacheManagerKey['currentclass'].toString());
    if (_reportsController.quizBookItems.isNotEmpty) {
      _reportsController.quizBookItems.clear();
    }
    Future.delayed(Duration(milliseconds: 200), () {
      _reportsController.getQuizBookRecordsByClass(classId, "", "", "");
    });
    super.initState();
  }

  @override
  void dispose() {
    _reportsController.quizBookItems.clear();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build teacher report QuizBook');

    return Scaffold(
        backgroundColor: Colors.transparent,
        body: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
          //椭圆形选项背景
          Padding(
            padding: EdgeInsets.only(top: 6.0, bottom: 15.0),
            child: FormBuilderChoiceChip(
              alignment: WrapAlignment.end,
              labelPadding: EdgeInsets.symmetric(horizontal: 21.0),
              selectedColor: Get.isDarkMode
                  ? Get.theme.primaryColorDark
                  : AppTheme.colors.orangeBtn,
              spacing: 3.0,
              decoration: InputDecoration.collapsed(
                hintText: '',
                border: InputBorder.none,
              ),
              labelStyle: TextStyle(
                color:
                    Get.isDarkMode ? Colors.black : AppTheme.colors.lightOrange,
                fontSize: 15,
                letterSpacing: 0.8,
              ),
              backgroundColor:
                  Get.isDarkMode ? Get.theme.primaryColor : Colors.grey,
              name: 'quizBook_type_chip',
              onChanged: (value) {
                var classId =
                    box.get(CacheManagerKey['currentclass'].toString());
                if (value != null) {
                  var eType = value as AssessmentBookType;
                  _reportsController.getQuizBookRecordsByClass(
                      classId, eType.assessmentType!, "", "");
                } else {
                  _reportsController.getQuizBookRecordsByClass(
                      classId, "", "", "");
                }
              },
              options: Constants.quizBookTypes
                  .map((bookType) => FormBuilderChipOption<AssessmentBookType>(
                        value: bookType,
                        child: Text(
                          bookType.displayName.toString(),
                        ),
                      ))
                  .toList(),
            ),
          ),
          //此grid的间距(Padding/margin)在testDataRecordGrid文件中设定。此处设定间距，grid被截一半
          _reportsController.obx(
            (data) => TestDataRecordGrids(
                _reportsController.quizBookItems, true, 'quizBook'),
            onEmpty: TestDataRecordGrids([], true, 'quizBook'),
            onLoading: Container(),
          ),
        ]));
  }
}
